<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.name" placeholder="店铺名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="selectshops">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="Shopexport">导出</el-button>
        </el-form-item>
        <el-form-item>
          <!-- 默认name="file" -->
          <el-upload class="upload-demo"
                     action="http://localhost:8080/shop/importExcel"
                     list-type="text">
            <el-button type="warning">点击导入</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="shops" highlight-current-row v-loading="listLoading"
              @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" label="序号" width="60">
      </el-table-column>
      <el-table-column prop="name" label="店铺名称" width="150">
      </el-table-column>
      <el-table-column prop="tel" label="店铺座机" width="200">
      </el-table-column>
      <el-table-column prop="registerTime" label="入驻时间" width="200">
      </el-table-column>
      <el-table-column prop="address" label="店铺地址" width="250">
      </el-table-column>
      <el-table-column prop="state" label="状态" width="80" sortable>
        <template scope="scope">
          <span v-if="scope.row.state==1" style="color: hotpink">待审核</span>
          <span v-if="scope.row.state==2" style="color: blue">待激活</span>
          <span v-if="scope.row.state==3" style="color: green">正常使用</span>
          <span v-if="scope.row.state==4" style="color: red">驳回</span>
        </template>
      </el-table-column>
      <el-table-column prop="logo" label="店铺logo" width="250" e>
      </el-table-column>
      <el-table-column prop="username" label="店铺管理员" width="150">
      </el-table-column>
      <el-table-column label="操作" width="250">
        <template scope="scope">
          <el-button size="small" type="info" disabled @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          <el-button size="small" type="danger" disabled @click="handleDel(scope.$index, scope.row)">删除</el-button>
          <el-button type="warning" :disabled="scope.row.state!==1" size="small"
                     @click="handleAudit(scope.$index, scope.row)">店铺审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :total="totals"
                     :current-page="currentPage"
                     style="float:right;">
      </el-pagination>
    </el-col>
    <!--新增界面-->
    <!-- 添加或修改的对话框
    el-dialog - elementUi提供的对话框 = 模态框
      :title="title" - 动态改变标题
      :visible.sync="saveFormVisible"  - 模态框的显示状态
      :close-on-click-modal="false" - 关闭模态框只能通过模态框的x关闭，点击空白处不能关闭模态框
    el-form - 表单
      :model="saveForm" - 绑定表单对象
      label-width="80px" - 标签宽度
      :rules="saveFormRules" - 验证规则
      ref="saveForm" - 为当前模态框取名
      prop="name" - 如果当前字段要验证，就必须写，出现*
      auto-complete="off" - 不自动提示
-->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="店铺名称" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="店铺座机" prop="tel">
          <el-input v-model="saveForm.tel" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="saveForm.address" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
              v-model="saveForm.state"
              active-color="#13ce66"
              inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
        <el-form-item prop="logo" label="店铺Logo">
          <!--
              :on-preview="handlePreview" - 点击图片名时触发此函数 - 可以在这里做放大功能，这里不做
              :on-remove="handleRemove" - 点击删除时触发
              :on-success="handleSuccess" - 上传成功之后触发
              :file-list="fileList" - 文件列表
              action="http://localhost:8080/fastDfs/" - 文件上传接口地址
          -->
          <el-upload
              class="upload-demo"
              action="http://localhost:8080/fastDfs/"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :on-success="handleSuccess"
              :file-list="fileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="管理员">
          <el-select v-model="saveForm.adminId" clearable placeholder="请选择">
            <el-option
                v-for="item in emps"
                :key="item.id"
                :label="item.username"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>
    <!-- 审核模态框 -->
    <el-dialog title="审核店铺" :visible.sync="shopAuditVisible" :close-on-click-modal="false">
      <el-form :model="shopAuditLog" label-width="80px" ref="shopAuditLogForm">
        <el-form-item label="备注" prop="note">
          <el-input type="textarea" v-model="shopAuditLog.note"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="shopAuditVisible = false">取消</el-button>
        <el-button type="primary" @click.native="auditPass">通过</el-button>
        <el-button type="primary" @click.native="auditReject">驳回</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import util from '../../common/js/util'
//import NProgress from 'nprogress'
import {getUserListPage, removeUser, batchRemoveUser, editUser, addUser} from '../../api/api';

export default {
  data() {
    return {
      filters: {
        name: ''
      },
      emps: [],
      shops: [],
      options: [],
      totals: 0,
      currentPage: 1,
      pageSize: 5,
      title: '',
      listLoading: false,
      sels: [],//列表选中列

      saveFormVisible: false,//编辑界面是否显示
      saveLoading: false,
      saveFormRules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ]
      },
      //编辑界面数据
      saveForm: {
        name: '',
        tel: '',
        state: '',
        address: '',
        logo: '',
        adminId: '',
      },
      fileList: [],
      //审核模态框
      shopAuditVisible: false,
      //编辑界面数据
      shopAuditLog: {
        shopId: null,
        note: ''
      },
    }
  },
  methods: {
    //文件上传的业务逻辑
    handleSuccess(response, file, fileList){
      //Object { success: true, message: "操作成功", resultObj: "/group1/M00/00/0F/oYYBAGJ6JV2ANENZAAhNlgqHjro238.gif" }
      console.log(response);
      this.saveForm.logo = response.resultObj;
    },

    //文件删除的业务逻辑
    handleRemove(file, fileList) {
      // Object {response: Object { success: true, message: "操作成功", resultObj: "/group1/M00/00/0F/oYYBAGJ6JmyATaYbAAk1mWiyf0E903.gif" }}
      console.log(file);
      var filePath =file.response.resultObj;
      this.$http.delete("/fastDfs?path="+filePath).then(res=>{
        if(res.data.success){
          this.$message({
            message: '删除成功!',
            type: 'success'
          });
        }else{
          this.$message({
            message: '删除失败!',
            type: 'error'
          });
        }
      })
    },
    handlePreview(file) {
      console.log(file);
    },
    //点击店铺审核弹出模态框
    handleAudit: function (index, row) {
      this.shopAuditLog.shopId = row.id;
      this.shopAuditVisible = true;
    },
//审核驳回
    auditReject(){
      this.$http.post("/shop/audit/reject",this.shopAuditLog).then(res=>{
        if(res.data.success){
          this.$message({
            message: '驳回成功',
            type: 'success'
          });
        }else{
          this.$message({
            message: '驳回失败',
            type: 'error'
          });
        }
        this.shopAuditVisible = false;
        this.getShops();
      })
    },
    //审核通过
    auditPass(){
      this.$http.post("/shop/audit/pass",this.shopAuditLog).then(result=>{
        result = result.data;
        if(result.success){
          this.$message({
            message: '审核通过',
            type: 'success'
          });
        }else{
          this.$message({
            message: '审核失败',
            type: 'error'
          });
        }
        this.shopAuditVisible = false;
        this.getShops();
      })
    },
    selectshops() {
      this.currentPage = 1;
      this.getshops();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getshops();
    },
    /*getShops(){
      this.$http.get("/shop").then((res) => {
        if(res.data.success){
          this.shops = res.data.resultObj;
          this.listLoading = false;
        }
      }).catch((res=>{
        this.$message.error("网络繁忙");
      }));
    },*/
    //获取用户列表
    getEmps() {
      this.$http.get("/emp").then((res) => {
        if (res.data.success) {
          this.emps = res.data.resultObj;
          this.listLoading = false;
        }
      }).catch((res => {
        this.$message.error("网络繁忙");
      }));
    },
    //获取店铺列表
    getshops() {
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.filters.name
      };
      this.listLoading = true;
      this.$http.post("/shop/pageList", para).then((res) => {
        if (res.data.success) {
          this.shops = res.data.resultObj.data;
          this.totals = res.data.resultObj.totals;
          this.listLoading = false;
        }
      }).catch((res => {
        this.$message.error("网络繁忙");
      }));
    },
    //删除
    handleDel: function (row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/shop/" + row.id).then((res) => {
          this.listLoading = false;
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getshops();
        });
      }).catch(() => {

      });
    },
    //显示编辑界面
    handleEdit: function (row) {
      this.saveFormVisible = true;
      this.title = '编辑';
      this.saveForm = Object.assign({}, row);
      console.log(this.saveForm)
    },
    Shopexport:function (){
      location.href="http://localhost:8080/shop/export";
    },
    //显示新增界面
    handleAdd: function () {
      this.saveFormVisible = true;
      this.title = '新增';
      this.saveForm = {
        name: '',
        tel: '',
        state: '',
        address: '',
        logo: '',
        adminId: '',
      };
    },
    //新增
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.saveLoading = true;
            let para = Object.assign({}, this.saveForm);
            //状态赋值
            para.state = para.state ? 1 : 0
            this.$http.post("/shop", para).then((res) => {
              if (res.data.success) {
                this.saveLoading = false;
                this.$message({
                  message: '提交成功',
                  type: 'success'
                });
                this.$refs['saveForm'].resetFields();
                this.saveFormVisible = false;
                this.getshops();
              }
            }).catch(res => {
              this.$message.error("请求失败");
            });
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        console.log(ids)
        //let para = ids[] ;
        this.$http.patch("/shop", ids).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getshops();
        });
      }).catch(() => {

      });
    }
  },
  mounted() {
    this.getshops();
    this.getEmps();
  }
}

</script>

<style scoped>

</style>